package com.langsin.action;

import com.langsin.service.ProductService;
import com.langsin.service.impl.ProductServiceImpl;
import com.langsin.util.Page;
import com.langsin.vo.Product;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProductListAction extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        ProductService service = new ProductServiceImpl();
        List<Product> list = null;
        HttpSession session = request.getSession();
        Page<Product> page = null;
        String pname = request.getParameter("pname");
        String pcode = request.getParameter("pcode");
        //区分用户操作类型
        String type = request.getParameter("type");

        if(type != null){
            if(session.getAttribute("queryState") == null){
                if("first".equals(type)){

                }
                if("last".equals(type)){

                }
                if("prev".equals(type)){

                }
                if("next".equals(type)){

                }
            }else{

            }
        }else{
            if(pname == null){

                list = service.queryAll();

            }else{
                Map<String,String> map = new HashMap<String,String>();
                map.put("pname",pname);
                map.put("pcode",pcode);
                session.setAttribute("queryState",map);
                list= service.queryListByName(pname,pcode);
            }
        }

        if(session.getAttribute("page") == null){
            page = new Page<Product>(4);
            session.setAttribute("page",page);
        }else {
            page = (Page<Product>)session.getAttribute("page");
        }


        request.setAttribute("productList",list);
        request.getRequestDispatcher("./productList.jsp").forward(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
